package cn.rengy.web.framework.shiro.config;
//package com.uniontrip.framework.shiro.config;
//
//
//import java.util.Arrays;
//import java.util.LinkedHashMap;
//import java.util.List;
//import java.util.Map;
//
//import javax.annotation.PostConstruct;
//import javax.servlet.Filter;
//
//import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
//import org.apache.shiro.web.filter.mgt.DefaultFilterChainManager;
//import org.apache.shiro.web.servlet.AbstractShiroFilter;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import com.uniontrip.framework.service.DbService;
//
//@Component
//public class FilterChainDefinitionMapManager {
//
//    private static final Logger LOGGER = LoggerFactory.getLogger(FilterChainDefinitionMapManager.class);
//    @Autowired
//    private ShiroFilterFactoryBean shiroFilterFactoryBean;
//    private Map<String,String> filterChainDefinitionMap;
//    @Autowired
//    private DbService dbService;
//    @PostConstruct
//    public void init(){
//
//    	this.filters = new LinkedHashMap<String, Filter>();
//        this.filterChainDefinitionMap = new LinkedHashMap<String, String>();
//    	
//    	
//    	
//    	filterChainDefinitionMap=shiroFilterFactoryBean.getFilterChainDefinitionMap();
//    	//初始化URL和过滤器匹配规则
//    	List<Map<String,Object>> list=this.dbService.queryListBySqlId("select_all_auth_resource");
//    	list.forEach(map -> filterChainDefinitionMap.put((String)map.get("uri"),(String)map.get("roles")));
//    }
//    
//    // 初始化静态过滤链规则
//    public Map<String,String> initStaticFilterChainDefinition() {
//        Map<String,String> filterChain = new LinkedHashMap<>();
//        // -------------anon 默认过滤器忽略的URL
//        List<String> defalutAnon = Arrays.asList("/css/**","/js/**");
//        defalutAnon.forEach(ignored -> filterChain.put(ignored,"anon"));
//        // -------------auth 默认需要认证过滤器的URL 走auth--PasswordFilter
//        List<String> defalutAuth = Arrays.asList("/account/**");
//        defalutAuth.forEach(auth -> filterChain.put(auth,"auth"));
//        // -------------dynamic 动态URL
//        if (shiroFilterRulesProvider != null) {
//            List<RolePermRule> rolePermRules = this.shiroFilterRulesProvider.loadRolePermRules();
//            if (null != rolePermRules) {
//                rolePermRules.forEach(rule -> {
//                    StringBuilder Chain = rule.toFilterChain();
//                    if (null != Chain) {
//                        filterChain.putIfAbsent(rule.getUrl(),Chain.toString());
//                    }
//                });
//            }
//        }
//        return filterChain;
//    }
//    // 动态重新加载过滤链规则
//    public void reloadFilterChain() {
//            ShiroFilterFactoryBean shiroFilterFactoryBean = SpringContextHolder.getBean(ShiroFilterFactoryBean.class);
//            AbstractShiroFilter abstractShiroFilter = null;
//            try {
//                abstractShiroFilter = (AbstractShiroFilter)shiroFilterFactoryBean.getObject();
//                RestPathMatchingFilterChainResolver filterChainResolver = (RestPathMatchingFilterChainResolver)abstractShiroFilter.getFilterChainResolver();
//                DefaultFilterChainManager filterChainManager = (DefaultFilterChainManager)filterChainResolver.getFilterChainManager();
//                filterChainManager.getFilterChains().clear();
//                shiroFilterFactoryBean.getFilterChainDefinitionMap().clear();
//                shiroFilterFactoryBean.setFilterChainDefinitionMap(this.initGetFilterChain());
//                shiroFilterFactoryBean.getFilterChainDefinitionMap().forEach((k,v) -> filterChainManager.createChain(k,v));
//            }catch (Exception e) {
//                LOGGER.error(e.getMessage(),e);
//            }
//    }
//    
//}
